package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.EventBusConfig;
import com.hemaapp.yjnh.bean.EventBusMsg;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.view.BaseButtonDialog;
import com.hemaapp.yjnh.view.BaseButtonDialog.OnButtonListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import xtom.frame.util.XtomSharedPreferencesUtil;
import xtom.frame.util.XtomToastUtil;

/**
 * 登录页
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.title_left_btn)
    ImageButton titleLeftBtn;
    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.edt_account)
    EditText edtAccount;
    @Bind(R.id.edt_pwd)
    EditText edtPwd;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.tv_forget)
    TextView tvForget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void findView() {
    }

    @Override
    protected void getExras() {
    }

    @Override
    protected void setListener() {
        titleText.setText("登录");
        titleLeftBtn.setImageResource(R.drawable.login_cancel);
        titleRightBtn.setText("注册");
    }

    @OnClick({R.id.title_left_btn, R.id.title_right_btn, R.id.btn_login, R.id.tv_forget})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.title_right_btn:
                Intent it = new Intent(mContext, Register0Activity.class);
                startActivity(it);
                break;
            case R.id.btn_login:
                String username = edtAccount.getText().toString().trim();
                String password = edtPwd.getText().toString().trim();
                if (isNull(username)) {
                    XtomToastUtil.showShortToast(mContext, "请输入手机号码");
                    return;
                }
                String mobile = "\\d{11}";// 只判断11位
                if (!username.matches(mobile)) {
                    XtomToastUtil.showShortToast(mContext, "请输入有效手机号码");
                    return;
                }
                if (password.length() < 6 || password.length() > 16) {
                    XtomToastUtil.showShortToast(mContext, "密码输入不正确，请输入6-16位密码");
                    return;
                }
                getNetWorker().clientLogin(username, password);
                break;
            case R.id.tv_forget:
                Intent itForget = new Intent(mContext, GetPassword0Activity.class);
                itForget.putExtra("keytype", "1");
                startActivity(itForget);
                break;
        }
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LOGIN:
                showProgressDialog("请稍候");
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LOGIN:
//		case THIRD_SAVE:
                cancelProgressDialog();
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult ZLGResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LOGIN:
                HemaArrayResult<User> uResult = (HemaArrayResult<User>) ZLGResult;
                User user = uResult.getObjects().get(0);
                getApplicationContext().setUser(user);
                String username = netTask.getParams().get("username");
                String password = netTask.getParams().get("password");
                XtomSharedPreferencesUtil.save(mContext, "username", username);
                XtomSharedPreferencesUtil.save(mContext, "password", password);
                EventBusMsg msg = new EventBusMsg();
                msg.setType(EventBusConfig.USER);
                msg.setUser(user);
                EventBus.getDefault().post(msg);
                finish();
                break;
            default:
                break;
        }

    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult result) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LOGIN:
                BaseButtonDialog dialog = new BaseButtonDialog(mContext);
                dialog.setText(result.getMsg());
                dialog.setRightButtonText("确定");
                dialog.setButtonListener(new OnButtonListener() {

                    @Override
                    public void onRightButtonClick(BaseButtonDialog dialog) {
                        dialog.cancel();
                    }

                    @Override
                    public void onLeftButtonClick(BaseButtonDialog dialog) {

                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LOGIN:
                XtomToastUtil.showShortToast(mContext, "登录失败，请稍后再试");
                break;
        }
    }
}
